package com.welsmann.app.start.spider.dao;

import java.util.List;

import com.welsmann.app.start.spider.dao.helper.QueryHelper;
import com.welsmann.app.start.spider.entity.NewsTitle;

/**
 * 新闻标题实体操作类
 * 
 * @author Welsmann
 * 
 */
public class NewsTitleDAO {

	/**
	 * 获取所有
	 * 
	 * @return
	 */
	public static List<NewsTitle> findAll() {
		return QueryHelper.list(NewsTitle.class,
				"select * from newstitle order by publish", null);
	}
	
	public static List<NewsTitle> findTop200() {
		return QueryHelper.list(NewsTitle.class, "select * from newstitle order by publish limit 200", null);
	}

	/**
	 * 根据主键获取
	 * 
	 * @param id
	 * @return
	 */
	public static NewsTitle find(String id) {
		return QueryHelper.single(NewsTitle.class,
				"select * from newstitle where id = ?", id);
	}

	/**
	 * 新增
	 * 
	 * @param nt
	 * @return
	 */
	public static int insert(NewsTitle nt) {
		return QueryHelper.update("insert into newstitle values(?,?,?,?,?,?)",
				nt.getId(), nt.getTitle(), nt.getPublish(), nt.getSource(),
				nt.getType(),nt.getLink());
	}

	/**
	 * 更新
	 * 
	 * @param nt
	 * @return
	 */
	public static int update(NewsTitle nt) {
		return QueryHelper
				.update("update newstitle set title = ?, publish = ?, source = ?, type = ?, link = ? where id = ?",
						nt.getTitle(), nt.getPublish(), nt.getSource(),
						nt.getType(), nt.getLink(), nt.getId());
	}

	/**
	 * 对象删除
	 * 
	 * @param nt
	 * @return
	 */
	public static int delete(NewsTitle nt) {
		return QueryHelper.update("delete from newstitle where id  = ? ",
				nt.getId());
	}

	/**
	 * 对象删除
	 * 
	 * @param id
	 * @return
	 */
	public static int delete(String id) {
		return QueryHelper.update("delete from newstitle where id  = ? ", id);
	}
	
	public static NewsTitle getLatestPost(String tId) {
		return QueryHelper.single(NewsTitle.class, "select * from newstitle where type = ? order by publish desc limit 1", tId);
	}

}
